﻿using System;
using System.Net;
using System.Text;
using System.Windows;
using eLearning.Controls;
using eLearning.Model;
using eLearning.Util;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Newtonsoft.Json;
using Coding4Fun.Toolkit.Controls;
using System.Windows.Input;
using System.Collections.ObjectModel;
using System.Windows.Controls;

namespace eLearning.DosenView
{
    public partial class PerkuliahanDetailPage : PhoneApplicationPage
    {
        private DetailMatkul detMatkul = null;

        public PerkuliahanDetailPage()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(DataContextPage_Loaded);
            ManageButton();
        }

        private void DataContextPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (DataContext is DetailMatkul)
            {
                detMatkul = (DetailMatkul)DataContext;
                if (detMatkul != null)
                {
                    pivotPerkuliahan.Header = detMatkul.Matkul.Nama;
                    //ObservableCollection<String> menu = new ObservableCollection<String>();
                    //menu.Add("Member");
                    //menu.Add("Materi");
                    //menu.Add("Tugas");
                    //menu.Add("Quiz");
                    //menu.Add("Soal");
                    //menu.Add("Pengumuman");
                    //listMenu.ItemsSource = menu;
                }
            }
        }

        private void DeleteCurrentPerkuliahan()
        {
            string json = null, url = null;

            if (detMatkul != null)
            {
                url = Config.Instance.PERKULIAHAN_SERVICE_URL() + Config.DELETE_ACTION_KEY;
                json = JsonConvert.SerializeObject(detMatkul);

                Uri uri = new Uri(url, UriKind.Absolute);
                WebClient client = new WebClient();

                client.Encoding = Encoding.UTF8;
                client.Headers[HttpRequestHeader.ContentType] = "application/json";
                client.Headers[HttpRequestHeader.ContentLength] = json.Length.ToString();

                client.UploadStringCompleted += new UploadStringCompletedEventHandler(deletePerkuliahan_UploadStringCompleted);

                try
                {
                    client.UploadStringAsync(uri, "POST", json);
                }
                catch (Exception) { }
            }
        }

        private void deletePerkuliahan_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
        {
            try
            {
                if (e.Cancelled && e.Error != null)
                    return;

                MessageBox.Show("Message : " + e.Result);
                if (e.Result.Contains("Success"))
                    NavigationService.GoBack();

            }
            catch (Exception ex)
            {
                MessageBox.Show("ERROR : " + ex.Message);
            }
        }

        private void buttonMember_Click(object sender, RoutedEventArgs e)
        {
            Utility.GotoPage(this, "/View/Dosen/ListMemberPage.xaml");

            PhoneApplicationService.Current.State[Config.DETMATKUL] = detMatkul;
        }

        private void buttonMateri_Click(object sender, RoutedEventArgs e)
        {
            Utility.GotoPage(this, "/View/Dosen/ResourcesPage.xaml");

            PhoneApplicationService.Current.State[Config.DETMATKUL] = detMatkul;
        }

        private void buttonTugas_Click(object sender, RoutedEventArgs e)
        {
            Utility.GotoPage(this, "/View/Dosen/TugasPage.xaml");

            PhoneApplicationService.Current.State[Config.DETMATKUL] = detMatkul;
        }

        private void buttonQuiz_Click(object sender, RoutedEventArgs e)
        {
            Utility.GotoPage(this, "/View/Dosen/QuizPage.xaml");

            PhoneApplicationService.Current.State[Config.DETMATKUL] = detMatkul;
        }

        private void buttonPengumuman_Click(object sender, RoutedEventArgs e)
        {
            Utility.GotoPage(this, "/View/PengumumanMatkulPage.xaml");
            PhoneApplicationService.Current.State[Config.DETMATKUL] = detMatkul;
        }

        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            base.OnBackKeyPress(e);
            NavigationService.RemoveBackEntry();
            Utility.GotoPage(this, "/View/Dosen/PerkuliahanPage.xaml");
        }

        private void editButton_Click(object sender, System.EventArgs e)
        {
            Utility.GotoPage(this, "/View/Dosen/PerkuliahanFormPage.xaml");
            FrameworkElement root = Application.Current.RootVisual as FrameworkElement;
            root.DataContext = detMatkul;
        }

        private void importButton_Click(object sender, EventArgs e)
        {
            Utility.GotoPage(this, "/Controls/CsvParser.xaml");
            PhoneApplicationService.Current.State[Config.DETMATKUL] = detMatkul;
        }

        private void ManageButton()
        {
            if (SavedUser.Instance.User != null && SavedUser.Instance.User.Type == User.UserType.Admin)
                ApplicationBar.IsVisible = true;
            else
                ApplicationBar.IsVisible = false;
        }

        private void listMenu_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
            {
                ListBoxItem menu = (ListBoxItem)e.AddedItems[0];
                switch (menu.Name)
                {
                    case "Member":
                        Utility.GotoPage(this, "/View/Dosen/ListMemberPage.xaml");
                        break;
                    case "Materi":
                        Utility.GotoPage(this, "/View/Dosen/ResourcesPage.xaml");
                        break;
                    case "Tugas":
                        Utility.GotoPage(this, "/View/Dosen/TugasPage.xaml");
                        break;
                    case "Quiz":
                        Utility.GotoPage(this, "/View/Dosen/QuizPage.xaml");
                        break;
                    case "Soal":
                        Utility.GotoPage(this, "/View/Dosen/QuestionPage.xaml");
                        break;
                    case "Pengumuman":
                        Utility.GotoPage(this, "/View/PengumumanMatkulPage.xaml");
                        break;
                    default:
                        break;
                }
                ((ListBox)sender).SelectedIndex = -1;
                PhoneApplicationService.Current.State[Config.DETMATKUL] = detMatkul;
            }
        }

    }
}